package com.icexxx.mapper_gen;

import java.util.ArrayList;
import java.util.List;

public class UpdateGen implements CodeGen {

    @Override
    public String genXml(String name, String comment, List<Param> paramList, Param returnParam, String nl) {
        String packageName = CodeContext.getPackageName();
        String genericity = XmlGen.genericity(paramList);
        if (genericity == null) {
            genericity = XmlGen.simpleType(paramList);
        }
        String pojoName = XmlGen.pojoName(packageName, genericity);
        StringBuilder sum = new StringBuilder();
        sum.append("    " + XmlGen.comment(comment) + nl);
        sum.append("     <update id=\"" + name + "\" parameterType=\"" + pojoName + "\">" + nl);
        List<String> paramWrapList = new ArrayList<>();
        for (int i = 0; i < paramList.size(); i++) {
            Param param = paramList.get(i);
            String paramName = param.getName();
            String paramWrap = XmlGen.paramWrap(paramName);
            paramWrapList.add(paramWrap);
        }
        String mainCode = XmlGen.mainCode("    ", paramWrapList);
        sum.append("    " + mainCode + nl);
        sum.append("    </update>" + nl);
        return sum.toString();
    }

}
